<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
            和岛屿数量差不多，也可以用dfs或者bfs。区别在于：当遍历到1的时候，让当前sun==0 ，然后dfs周围，每遇到一个1就count++
            */
            var maxAreaOfIsland = function (grid) {
                let m = grid.length
                let n = grid[0].length
                let result = 0
                let count = 0
                for (let i = 0; i < m; i++) {
                    for (let j = 0; j < n; j++) {
                        if (grid[i][j] == 1) {
                            count = 0
                            dfs(i, j)
                            result = Math.max(count, result)
                        }
                    }
                }
                return result
                function dfs(x, y) {
                    //终止条件
                    if (x < 0 || y < 0 || x >= m || y >= n || grid[x][y] == 0) {
                        return
                    }
                    count++
                    grid[x][y] = 0
                    dfs(x - 1, y)
                    dfs(x + 1, y)
                    dfs(x, y + 1)
                    dfs(x, y - 1)
                }
            }
        </script>
    </body>
</html>
